<?php

namespace App\Providers;

use App\Handler\DailyLogHandler;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to your application's "home" route.
     *
     * Typically, users are redirected here after authentication.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * Define your route model bindings, pattern filters, and other route configuration.
     */
    public function boot(): void
    {
        try {
            //为框架日志添加链路查询标识
            DailyLogHandler::setLogThreadId();
        } catch (\Throwable $e) {
        }
        //        RateLimiter::for('api', function (Request $request) {
        //            return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
        //        });
        Route::middleware('web')->group(base_path('routes/web.php'));
        Route::namespace($this->namespace)->group(base_path('routes/client.php'));

        $this->routes(function () {
            Route::prefix('callback')
                ->namespace($this->namespace)
                ->group(base_path('routes/callback.php'));
        });
    }

    /**
     * Configure the rate limiters for the application.
     */
    protected function configureRateLimiting(): void
    {
        RateLimiter::for('client', function (Request $request) {
            return Limit::perMinute(100)
                ->by(optional($request->user())->id ?: $request->ip())
                ->response(function () {
                    return response('操作频繁，请稍后重试');
                });
        });
    }
}
